package com.edgclub.edg.activity;

import android.os.Bundle;
import android.os.Environment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.event.BroadCastEvent;
import com.edgclub.edg.event.EventBus;
import com.edgclub.edg.uc.galleryviewpager.BasePagerAdapter.OnItemChangeListener;
import com.edgclub.edg.uc.galleryviewpager.GalleryViewPager;
import com.edgclub.edg.uc.galleryviewpager.UrlPagerAdapterforImage;
import com.edgclub.edg.utils.GlideUtil.ImageLoaderClient;
import com.edgclub.edg.utils.util.StringUtils;
import com.squareup.otto.ThreadEnforcer;

import java.util.ArrayList;

import me.nereo.multi_image_selector.bean.Image;

/**
 * 图集
 * 
 * @author eric
 * 
 */
public class GzImageGalleryViewPagerTopicConfirmActivity extends GzBaseTooolBarActivity
		implements OnClickListener {

	private GalleryViewPager mViewPager;
	private ArrayList<Image> urlsList;
	private ArrayList<Image> seleteUrls=new ArrayList<>();
	private int number = 8;
	private TextView image_ok;
	private ArrayList<String> imageSize=new ArrayList<>();
	private int index = 0;
	private Image default_url;// 从外面传进来的默认url

	private UrlPagerAdapterforImage pagerAdapter;
	private ArrayList<String> list = new ArrayList<>();

	private ImageLoaderClient imageLoaderClient;

	private TextView send_picture;

	private TextView tvIndex ,image_choice_text_ytsize,image_choice_text_yt;

	private ImageView image_choice,image_choice_yt;

	private TextView image_choice_text;
	public static final String SAVEIMAGEPATH = Environment
			.getExternalStorageDirectory() + "/guzhi/saveImages/";
	
	private LinearLayout back;
	public static final String tag="-ori";

	private java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#0.00");


	@Override
	public int getContentView() {
		return R.layout.activity_gz_imagegallery_topic_confirmation;
	}

	@Override
	public void setTitleBarView() {
		EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
				ThreadEnforcer.MAIN).register(this);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
				ThreadEnforcer.MAIN).unregister(this);
		try {
			pagerAdapter.destory();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public void initView() {

			findview();
			initInstance();

			imageLoaderClient = new ImageLoaderClient(this);

		}

	@Override
	public void initData() {

	}

	private void findview() {
		tvIndex = (TextView) findViewById(R.id.tvIndex);
		image_choice_text_ytsize= (TextView) findViewById(R.id.image_choice_text_ytsize);
		image_choice_text_yt= (TextView) findViewById(R.id.image_choice_text_yt);
		image_choice_yt= (ImageView) findViewById(R.id.image_choice_yt);
		mViewPager = (GalleryViewPager) findViewById(R.id.viewer);
		image_ok = (TextView) findViewById(R.id.image_ok);
		image_ok.setOnClickListener(this);
		image_choice = (ImageView) findViewById(R.id.image_choice);
		image_choice.setOnClickListener(this);
		image_choice_text = (TextView) findViewById(R.id.image_choice_text);
		image_choice_text.setOnClickListener(this);
		image_choice_yt.setOnClickListener(this);
		image_choice_text_yt.setOnClickListener(this);
		back = (LinearLayout) findViewById(R.id.back);
		back.setOnClickListener(this);
	}

	private void initInstance() {
		Bundle bundle = getIntent().getBundleExtra("bundle");
		seleteUrls = (ArrayList<Image>) bundle.getSerializable("seleteUrls");
		number = bundle.getInt("number");
		if (seleteUrls.size() > 0) {
			image_ok.setText(StringUtils.getString(R.string.finish) + "(" + seleteUrls.size() + "/" + number
					+ ")");
		}
		imageSize=bundle.getStringArrayList("imageSize");
		urlsList = (ArrayList<Image>) bundle.getSerializable("urls");
		default_url = (Image) bundle.getSerializable("default_url");
		index = bundle.getInt("index");// 直接传当前页索引过来
		try {
			setSelectMeth(index);
		} catch (Exception e) {
			e.printStackTrace();
		}
		for (int i = 0; i < urlsList.size(); i++) {
			list.add(urlsList.get(i).path);
		}
		pagerAdapter = new UrlPagerAdapterforImage(this, list);
		pagerAdapter.setOnItemChangeListener(new OnItemChangeListener() {
			@Override
			public void onItemChange(int currentPosition) {
				tvIndex.setText((currentPosition + 1) + "/" + urlsList.size());
			}
		});
		mViewPager.setOffscreenPageLimit(2);
		mViewPager.setAdapter(pagerAdapter);

		mViewPager
				.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

					@Override
					public void onPageSelected(final int arg0) {
						index = arg0;
						setSelectMeth(index);

					}

					@Override
					public void onPageScrolled(int arg0, float arg1, int arg2) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onPageScrollStateChanged(int arg0) {
						// TODO Auto-generated method stub

					}
				});
		mViewPager.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View paramView) {

			}
		});

		mViewPager.setCurrentItem(index);
	}

	private void setSelectMeth(int index){
		Image image = urlsList.get(index);
		if(seleteUrls.contains(image)){
			for(int i=0;i<seleteUrls.size();i++){
				if(image.path.equals(seleteUrls.get(i).path)){
					image_choice.setImageResource(R.drawable.ic_gz_image_select);
					if(seleteUrls.get(i).isOri){
						image_choice_yt
								.setImageResource(R.drawable.ic_gz_image_select);
//						if(null!=imageSize&&imageSize.size()>index)
							image_choice_text_ytsize.setText("("+df.format(seleteUrls.get(i).getSize()/1024.0/1024.0+0.01)+"M)");
					}else{
						image_choice_yt
								.setImageResource(R.drawable.ic_gz_image_unselect);
						image_choice_text_ytsize.setText("");
					}
					break;
				}else{
					image_choice
							.setImageResource(R.drawable.ic_gz_image_unselect);
				}
			}
		}else{
			image_choice.setImageResource(R.drawable.ic_gz_image_unselect);
			image_choice_yt
					.setImageResource(R.drawable.ic_gz_image_unselect);
			image_choice_text_ytsize.setText("");
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.image_ok:
			setResult(RESULT_OK, getIntent().putExtra("seleteUrls", seleteUrls).putExtra("isfinish", true));
			finish();
			break;
		case R.id.image_choice_yt:
		case R.id.image_choice_text_yt:

			Image image1 = urlsList.get(index);

			if (image1.isOri) {
				// 已选择，则去掉
				image_choice_yt.setImageResource(R.drawable.ic_gz_image_unselect);
				image1.isOri = false;
				image_choice_text_ytsize.setText("");

			} else {
				if(seleteUrls.contains(image1)){
					seleteUrls.remove(image1);
				}
				image1.isOri = true;
				if (seleteUrls.size() == number) {
					showToast(StringUtils.getString(R.string.canchooseto) + number + StringUtils.getString(R.string.zhang));
					return;
				}
				seleteUrls.add(image1);
				image_choice.setImageResource(R.drawable.ic_gz_image_select);
				image_choice_yt.setImageResource(R.drawable.ic_gz_image_select);

			}
			image_ok.setText(StringUtils.getString(R.string.finish) + "(" + seleteUrls.size() + "/" + number
					+ ")");
//			if(null!=imageSize&&imageSize.size()>index)
				image_choice_text_ytsize.setText("("+df.format(image1.getSize()/1024.0/1024.0+0.01)+"M)");
			break;
		case R.id.image_choice:
		case R.id.image_choice_text:

			Image image = urlsList.get(index);

			if (seleteUrls.contains(image)) {
				// 已选择，则去掉
				image_choice.setImageResource(R.drawable.ic_gz_image_unselect);
				image_choice_yt.setImageResource(R.drawable.ic_gz_image_unselect);
				seleteUrls.remove(image);
			} else {
				if (seleteUrls.size() == number) {
					showToast(StringUtils.getString(R.string.canchooseto) + number + StringUtils.getString(R.string.zhang));
					return;
				}
				image_choice.setImageResource(R.drawable.ic_gz_image_select);
				seleteUrls.add(image);
			}
			image_ok.setText(StringUtils.getString(R.string.finish) + "(" + seleteUrls.size() + "/" + number
					+ ")");
			break;
		case R.id.back:
			setResult(RESULT_OK, getIntent().putExtra("seleteUrls", seleteUrls).putExtra("isfinish", false));
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		onClick(back);
	}


	// // 图片被点击
	// @Subscribe
	// public void bgChangeEvent(String event) {
	// if (BroadCastEvent.IMAGE_CLICK.equals(event)) {
	// finish();
	// }
	// }
}
